<?php
/**
 * Created by PhpStorm.
 * User: fang_
 * Date: 2016/9/30
 * Time: 10:39
 */
// 获取学生作业列表
require 'vendor/autoload.php';

use Sunra\PhpSimple\HtmlDomParser;

function getList()
{
    $client = new GuzzleHttp\Client();

    if (!empty($_GET['hwtid']) && !empty($_COOKIE['session_id'])) {
        // 需要

        $res = $client->request('POST',
            'http://e-learning.hznu.edu.cn/eol/common/hw/hwanswer.jsp?hwtid=' . $_GET['hwtid'] . '&restart=true',
            [
                'headers' => ['Cookie' => $_COOKIE['session_id']]
            ]
        );

        $html = HtmlDomParser::str_get_html(iconv('GBK', 'UTF-8', $res->getBody()));
//        echo $html;

        $unmark_array = array();

        $commit_count = $html->find('form[name=form1] div.navigation div.navigation_href b',0)->plaintext;

        $pages = ceil($commit_count / 50);

        for ($i=1;$i<=$pages;$i++)
        {
            $res = $client->request('POST',
                'http://e-learning.hznu.edu.cn/eol/common/hw/hwanswer.jsp?comment=3&hwtid=' . $_GET['hwtid'] . '&s_gotopage='.$i,
                [
                    'headers' => ['Cookie' => $_COOKIE['session_id']]
                ]
            );
            $html = HtmlDomParser::str_get_html(iconv('GBK', 'UTF-8', $res->getBody()));
            $table = $html->find('form[name=form1] table.infotable_hr tbody tr[align=center]');

            foreach ($table as $element) {
                $unmark_array[] = array(
                    'name' => $element->find('td', 2)->plaintext,
                    'student_id' => $element->find('td', 1)->plaintext,
                    'commit_time' => $element->find('td', 3)->plaintext,
                    'score' => trim($element->find('td', 4)->plaintext),
                    'mark_link' => substr(strstr($element->find('td', 8)->first_child()->href,'hwaid='),6)
                );
            }
        }


//    print_r(json_encode($unmark_array));
      return $unmark_array;
    }
    return null;

}


?>